package com.chanyachting.emall.serviceImpl;

import java.util.List;

import org.hibernate.Query;

import com.chanyachting.emall.dao.CompanyproductDAO;
import com.chanyachting.emall.domain.Company;
import com.chanyachting.emall.domain.Companyproduct;
import com.chanyachting.emall.service.CompanyproductService;
import com.chanyachting.emall.util.DateUtil;

public class CompanyproductServiceImpl implements CompanyproductService {

	private CompanyproductDAO cpDao;
	
	public CompanyproductServiceImpl() {
		
		this.cpDao=new CompanyproductDAO();
	}

	public boolean createCompanyproduct(Companyproduct cp) {
		
		this.cpDao.save(cp);
		
		return true;
	}

	public boolean delCompanyproduct(Companyproduct cp) {
		
		this.cpDao.delete(cp);
		
		return true;
	}

	public List<Companyproduct> getAllCompanyproduct() {
		
		return this.cpDao.find("from Companyproduct");
	}

	public List<Companyproduct> getAllPublishCompanyproduct() {
		
		return this.cpDao.find("from Companyproduct where status='01'");
	}

	public List<Companyproduct> getCompanyproductByCompany(Company company) {
		
		return this.cpDao.getCompanyproductByCompany(company);
	}

	@SuppressWarnings("unchecked")
	public Companyproduct getOneCompanyproduct(Companyproduct cp) {
		
//		return (Companyproduct) this.cpDao.load(Companyproduct.class, cp.getId());
		List<Companyproduct> tmp =  this.cpDao.find("from Companyproduct where id = ?", new String[]{cp.getId().toString()});
		
		if (tmp != null && tmp.size() != 0) {
			return tmp.get(0);
		}
		return null;
	}

	public List<Companyproduct> getPublishCompanyproductByCompany(
			Company company) {
		
		return this.cpDao.getPublishCompanyproductByCompany(company);
	}

	public boolean refreshCompanyproduct(Companyproduct cp) {
		
		cp.setRefreshDate(DateUtil.getDate());
		
		this.cpDao.update(cp);
		
		return true; //this.cpDao.update(Companyproduct.class,cp, cp.getId());
	}

	public boolean releaseCompanyproduct(Companyproduct cp) {
		
		cp.setStatus("01");
		
		this.cpDao.update(cp);
		
		return true;//this.cpDao.update(Companyproduct.class,cp, cp.getId());
	}

	public boolean underCompanyproduct(Companyproduct cp) {
		
		cp.setStatus("00");
		
		this.cpDao.update(cp);
		
		return true;//this.cpDao.update(Companyproduct.class,cp, cp.getId());
	}
	
	public boolean unPublicCompanyproduct(Companyproduct cp) {
		
		cp.setRecommend(0);
		
		this.cpDao.update(cp);
		
		return true;//this.cpDao.update(Companyproduct.class,cp, cp.getId());
	}
	
	public boolean publicCompanyproduct(Companyproduct cp) {
		
		cp.setRecommend(1);
		
		this.cpDao.update(cp);
		
		return true;
	}

	public boolean updateCompanyproduct(Companyproduct cp) {
		
		this.cpDao.update(cp);
		
		return true;//this.cpDao.update(Companyproduct.class,cp, cp.getId());
	}

	public List<Companyproduct> getUnderCompanyproductByCompany(Company company) {
		
		String args []={String.valueOf(company.getId())};

		return this.cpDao.find("from Companyproduct where companyId=? and status='00'", args);
	}

	public int getPublishCompanyproductCount() {
		String sql = "select count(*) from Companyproduct where status = '01'";
		
		return (Integer)this.cpDao.find(sql).get(0);
	}
	
	@SuppressWarnings("unchecked")
	public List getAllPublicCompanyproduct(int currentPage,int pageSize) {
		String sql = "from Companyproduct where status = '01'";
		Query q = this.cpDao.createQuery(sql);
		q.setMaxResults(pageSize);
		q.setFirstResult(currentPage * pageSize);
		List getObj = q.list();
		
		return q.list();
	}

	public List<Companyproduct> getPublishCompanyproductByCompany(
			Company company, String productType) {
		
		String args []={String.valueOf(company.getId()),productType};
		
		return this.cpDao.find("from Companyproduct where companyId=? and status='01' and type=?", args);
	}

	public List<Companyproduct> getUnderCompanyproductByCompany(
			Company company, String productType) {
		
		String args []={String.valueOf(company.getId()),productType};

		return this.cpDao.find("from Companyproduct where companyId=? and status='00' and type=?", args);
	}
	
	public List<Companyproduct> getRecommendCompanyproductByCompany(
			Company company, String productType) {
		
		String args []={String.valueOf(company.getId()),productType};

		return this.cpDao.find("from Companyproduct where companyId=? and recommend='1' and type=?", args);
	}
}
